package com.javalong.affairs.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.javalong.affairs.R;
import com.javalong.affairs.data.AllData;
import com.javalong.affairs.listener.BaseResponseListener;
import com.javalong.affairs.manager.IntentManager;
import com.javalong.affairs.model.User;
import com.javalong.affairs.ui.TopTitle;
import com.javalong.affairs.utils.HttpUtils;
import com.javalong.affairs.utils.UrlUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

@ContentView(R.layout.activity_userinfo)
public class UserInfoActivity extends BaseActivity {

	@ViewInject(R.id.ui_toptitle)
	private TopTitle topTitle;
	@ViewInject(R.id.tv_username)
	private TextView tv_username;
	@ViewInject(R.id.tv_workid)
	private TextView tv_workid;
	@ViewInject(R.id.tv_dept)
	private TextView tv_dept;
	@ViewInject(R.id.tv_phone)
	private TextView tv_phone;
	private User user;
	@ViewInject(R.id.bt_update)
	private Button bt_update;
	@ViewInject(R.id.bt_logout)
	private Button bt_logout;
	@ViewInject(R.id.bt_updatePwd)
	private Button bt_updatePwd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		topTitle.setOnLeftDrawableClick(this);
		initData();
		initUI();
	}

	private void initUI() {
		tv_workid.setText(user.workId);
		tv_username.setText(user.name);
		tv_dept.setText(user.section);
		tv_phone.setText(user.phone);
		bt_update.setOnClickListener(this);
		bt_updatePwd.setOnClickListener(this);
		bt_logout.setOnClickListener(this);
	}

	private void initData() {
		user = (User) AllData.GetInstance()
				.get(R.string.data_user_current_user);

	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.bt_update:
			updateUserInfo();
			break;
		case R.id.bt_logout:
			logout();
			break;
		case R.id.bt_updatePwd:
			updatePwd();
			break;
		default:
			break;
		}
	}

	private void updatePwd() {
		IntentManager.doActivity(this, UpdatePwdActivity.class);
	}

	private void logout() {
		progressDialog.show();
		String url = UrlUtils.getUrl("User", "logout");
		HttpUtils.post(url, null, new BaseResponseListener(null) {
			@Override
			public void onSuccess(Object obj) {
				super.onSuccess(obj);
				progressDialog.dismiss();
				IntentManager.doActivity(UserInfoActivity.this,
						LoginActivity.class);
			}
		});
	}

	private void updateUserInfo() {
		IntentManager.doActivity(this, UpdateUserInfoActivity.class);
	}
}
